package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityServiceItemsListBinding;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceNodeAdpater;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceProAdapter;
import com.sgb.kjwl.viewmodel.enterprise.ServiceNodeListModel;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.lang.reflect.Type;
import java.util.ArrayList;


/**
 * Time:2021/09/01
 * Author:陈方凯
 * Description:发布服务详情 - 服务项目列表
 */
public class ServiceProListActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {
    private ActivityServiceItemsListBinding mBinding;
    private ServiceNodeListModel mViewModel;
    //服务项目
    private ServiceProAdapter mAdapter;
    //服务流程节点
    private ServiceNodeAdpater serviceNodeAdpater;
    private String serviceId;//服务id
    private boolean isLodMore;
    private String json;
    private int type;  //0服务项目    1服务流程节点


    public static void startActivity(Context mContext, String serviceId, String json, int type) {
        Intent mIntent = new Intent(mContext, ServiceProListActivity.class);
        mIntent.putExtra("serviceId", serviceId);
        mIntent.putExtra("json", json);
        mIntent.putExtra("type", type);
        mContext.startActivity(mIntent);
    }

    @Override
    protected void initView() {
        mViewModel = new ViewModelProvider(this).get(ServiceNodeListModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_service_items_list);
        serviceId = getIntent().getStringExtra("serviceId");
        json = getIntent().getStringExtra("json");
        type = getIntent().getIntExtra("type", 0);
        onObserve();
        initAdapter();
    }

    private void initAdapter() {
        if (type == 0) {
            mBinding.actionBar.setTitle("服务项目列表");
            mAdapter = new ServiceProAdapter(this, null);
            mBinding.rvList.setLayoutManager(new LinearLayoutManager(this));
            mBinding.rvList.setAdapter(mAdapter);
        } else {
            mBinding.actionBar.setTitle("服务流程列表");
            serviceNodeAdpater = new ServiceNodeAdpater(this);
            mBinding.rvList.setLayoutManager(new LinearLayoutManager(this));
            DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
            divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide_05));
            mBinding.rvList.addItemDecoration(divider);
            mBinding.rvList.setAdapter(serviceNodeAdpater);
            mBinding.rvList.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    isLodMore = false;
                    mViewModel.getServiceNodeList(false, serviceId);
                }

                @Override
                public void onLoadMore() {
                    isLodMore = true;
                    mViewModel.getServiceNodeList(true, serviceId);
                }
            });
        }
    }

    @Override
    protected void init() {
        isLodMore = false;
        if (type == 0) {
            mBinding.rvList.setLoadingMoreEnabled(false);
            mBinding.rvList.setPullRefreshEnabled(false);
            //判断服务项目列表json是否为空
            if (!TextUtils.isEmpty(json)) {
                mAdapter.setDatas(mViewModel.servicePriceToBean(json).size() != 0 ? mViewModel.servicePriceToBean(json) : null);
                mBinding.include.setVisibility(View.GONE);
            } else {
                mAdapter.setDatasMayNull(null);
                mBinding.include.setVisibility(View.VISIBLE);
            }
        } else {
            mViewModel.getServiceNodeList(false, serviceId);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    private void onObserve() {
        mViewModel.serviceNodeList.observe(this, serviceNodeListEntities -> {
            if (isLodMore) {
                if (null != serviceNodeListEntities && serviceNodeListEntities.size() > 0) {
                    mBinding.rvList.loadMoreComplete();
                    serviceNodeAdpater.addDatas(serviceNodeListEntities);
                } else {
                    mBinding.rvList.setNoMore(true);
                }
                mBinding.rvList.setVisibility(View.VISIBLE);
            } else {
                mBinding.rvList.refreshComplete();
                if (null != serviceNodeListEntities && serviceNodeListEntities.size() > 0) {
                    mBinding.rvList.setVisibility(View.VISIBLE);
                    mBinding.include.setVisibility(View.GONE);
                    serviceNodeAdpater.setDatas(serviceNodeListEntities);
                    mBinding.rvList.setLoadingMoreEnabled(true);
                } else {
                    serviceNodeAdpater.setDatasMayNull(null);
                    mBinding.include.setVisibility(View.VISIBLE);
                }
            }
        });

        mViewModel.toast.observe(this, s -> {
            MToast.showToast(this, s);
        });
        mViewModel.ShowLoading.observe(this, integer -> {
            switch (integer) {
                case 1:
                    DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.itv_service_item:

                break;
        }
    }
}
